package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p = new Person("李四",22);
        System.out.println(p);

        Class cls = Class.forName("reflect.Person");
        //cls.getConstructor();//不传参数时,获取的就是无参构造器

        //Constructor对象的每一个实例用于表示一个构造器
        Constructor c = cls.getConstructor(String.class,int.class);//Person(String,int)必须和构造器里面类型,顺序一样
        Object obj = c.newInstance("王五",33);//new Person("王五",33);
        System.out.println(obj);


    }
}
